package com.demo.apparel.mapper.uniapp;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.apparel.dto.uniapp.GoodsWithAvatarDTO;
import com.demo.apparel.entity.Goods;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {

    @Insert("INSERT INTO goods " +
            "(id, clothes_id, user_id,shop_id, category, name, description, introduce, size, image_urls, season,price,status_sale, created_time, updated_time) " +
            "VALUES " +
            "(#{id}, #{clothesId}, #{userId}, #{shopId},#{category}, #{name}, #{description}, #{introduce}, #{size}, #{imageUrls}, #{season},#{price}, #{statusSale}, #{createdTime}, #{updatedTime})")
    void insertGoods(Goods goods);

//    @Select("<script>" +
//            "SELECT COUNT(*) FROM goods " +
//            "<where>" +
//            "  <if test='name != null and name.trim() != \"\"'> " +
//            "    name LIKE CONCAT('%', #{name}, '%') " +
//            "  </if>" +
//            "</where>" +
//            "</script>")
//    long countGoods(@Param("name") String name);
//
//    @Select("<script>" +
//            "SELECT g.*, u.avatar AS userAvatar " +
//            "FROM goods g " +
//            "LEFT JOIN user u ON g.user_id = u.id " +
//            "<where>" +
//            "  <if test='name != null and name.trim() != \"\"'> " +
//            "    AND g.name LIKE CONCAT('%', #{name}, '%') " +
//            "  </if>" +
//            "</where>" +
//            "ORDER BY g.created_time DESC " +
//            "LIMIT #{offset}, #{pageSize}" +
//            "</script>")
//    List<GoodsWithAvatarDTO> selectGoodsPage(@Param("offset") int offset,
//                                             @Param("pageSize") int pageSize,
//                                             @Param("name") String name);

//    @Select("<script>" +
//            "SELECT g.*, u.avatar AS userAvatar " +
//            "FROM goods g " +
//            "LEFT JOIN user u ON g.user_id = u.id " +
//            "<where>" +
//            "  <if test='name != null and name.trim() != \"\"'> " +
//            "    AND g.name LIKE CONCAT('%', #{name}, '%') " +
//            "  </if>" +
//            "  <if test='category != null and category.trim() != \"\"'> " +
//            "    AND g.category = #{category} " +
//            "  </if>" +
//            "</where>" +
//            "ORDER BY g.created_time DESC " +
//            "LIMIT #{offset}, #{pageSize}" +
//            "</script>")
//    List<GoodsWithAvatarDTO> selectGoodsPage(@Param("offset") int offset,
//                                             @Param("pageSize") int pageSize,
//                                             @Param("name") String name,
//                                             @Param("category") String category);
//
//
//    @Select("<script>" +
//            "SELECT COUNT(*) FROM goods " +
//            "<where>" +
//            "  <if test='name != null and name.trim() != \"\"'> " +
//            "    name LIKE CONCAT('%', #{name}, '%') " +
//            "  </if>" +
//            "  <if test='category != null and category.trim() != \"\"'> " +
//            "    AND category = #{category} " +
//            "  </if>" +
//            "</where>" +
//            "</script>")
//    long countGoods(@Param("name") String name, @Param("category") String category);
@Select("<script>" +
        "SELECT g.*, u.avatar AS userAvatar " +
        "FROM goods g " +
        "LEFT JOIN user u ON g.user_id = u.id " +
        "<where>" +
        "  <if test='name != null and name.trim() != \"\"'> " +
        "    AND (g.name LIKE CONCAT('%', #{name}, '%') " +
        "         OR g.introduce LIKE CONCAT('%', #{name}, '%') " +
        "         OR g.season LIKE CONCAT('%', #{name}, '%') " +
        "         OR g.description LIKE CONCAT('%', #{name}, '%')) " +
        "  </if>" +
        "  <if test='category != null and category.trim() != \"\"'> " +
        "    AND g.category = #{category} " +
        "  </if>" +
        "</where>" +
        "ORDER BY g.created_time DESC " +
        "LIMIT #{offset}, #{pageSize}" +
        "</script>")
List<GoodsWithAvatarDTO> selectGoodsPage(@Param("offset") int offset,
                                         @Param("pageSize") int pageSize,
                                         @Param("name") String name,
                                         @Param("category") String category);

    @Select("<script>" +
            "SELECT COUNT(*) FROM goods " +
            "<where>" +
            "  <if test='name != null and name.trim() != \"\"'> " +
            "    (name LIKE CONCAT('%', #{name}, '%') " +
            "     OR introduce LIKE CONCAT('%', #{name}, '%') " +
            "     OR season LIKE CONCAT('%', #{name}, '%') " +
            "     OR description LIKE CONCAT('%', #{name}, '%')) " +
            "  </if>" +
            "  <if test='category != null and category.trim() != \"\"'> " +
            "    AND category = #{category} " +
            "  </if>" +
            "</where>" +
            "</script>")
    long countGoods(@Param("name") String name, @Param("category") String category);


}
